#include "GY_Switch.h"
#include <QPen>

//运行状态激活区域点击功能没实现
GY_Switch::GY_Switch(const QString &guid, QGraphicsItem *parent) : GY_ContainerTransform(guid,this, parent)
{
    initializationPropertyInfo();
    this->setCenterPointF(m_PropertyInfoMap["Position"].propertyValue.toPointF());
    QPen pen;
    pen.setColor(Qt::red);
    pen.setStyle(Qt::DotLine);
}

bool GY_Switch::addItem(GY_Object *pItem)
{
    if(pItem->inherits("GY_BasicsGraphicsObject"))
    {
        this->addToGroup((GY_BasicsGraphicsObject*)pItem);
        //不是全部显示添加时隐藏
        if(!m_PropertyInfoMap["ShowAllStates"].propertyValue.toBool()) {
            ((GY_BasicsGraphicsObject*)pItem)->setVisible(false);
        }
    }
    else if(pItem->inherits("GY_ContainerTransform"))
    {
        this->addToGroup((GY_ContainerTransform*)pItem);
        if(!m_PropertyInfoMap["ShowAllStates"].propertyValue.toBool()) {
            ((GY_ContainerTransform*)pItem)->setVisible(false);
        }
    }
    else
    {
        return false;
    }
    m_OrderChildrens.append(pItem);
    m_ChildrensMap.insert(pItem->getGuid(),pItem);
    //下面属性不可主动更改，全部以图形数目为准
    m_PropertyInfoMap["FallbackState.Capacity"].propertyValue = m_OrderChildrens.size();
    m_PropertyInfoMap["FallbackState.Size"].propertyValue = m_OrderChildrens.size();
    m_PropertyInfoMap["ActiveArea.Capacity"].propertyValue = m_OrderChildrens.size();
    m_PropertyInfoMap["ActiveArea.Size"].propertyValue = m_OrderChildrens.size();
    return true;
}

bool GY_Switch::removItem(const QString &guid)
{
    if(!m_ChildrensMap.contains(guid))
    {
        return false;
    }
    m_ChildrensMap.remove(guid);
    return true;
}

bool GY_Switch::removItem(const GY_Object *pItem)
{
    if(pItem->inherits("GY_BasicsGraphicsObject"))
    {
        this->removeFromGroup((GY_BasicsGraphicsObject*)pItem);
    }
    else if(pItem->inherits("GY_ContainerTransform"))
    {
        this->removeFromGroup((GY_ContainerTransform*)pItem);
    }
    else
    {
        return false;
    }
    m_OrderChildrens.removeOne((GY_Object *)pItem);
    m_PropertyInfoMap["FallbackState.Capacity"].propertyValue = m_OrderChildrens.size();
    m_PropertyInfoMap["FallbackState.Size"].propertyValue = m_OrderChildrens.size();
    m_PropertyInfoMap["ActiveArea.Capacity"].propertyValue = m_OrderChildrens.size();
    m_PropertyInfoMap["ActiveArea.Size"].propertyValue = m_OrderChildrens.size();
    return  removItem(pItem->getGuid());
}

void GY_Switch::initializationPropertyInfo()
{
    GY_PropertyInfo ObjectName(this);
    ObjectName.propertyName = "ObjectName";
    ObjectName.propertyValue = QString();
    ObjectName.updateType = Update|OrdinaryAttributes;
    ObjectName.propertyType = QStringPropertyType;
    ObjectName.updatePropertyFun = (GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updateObjectName;
    m_PropertyInfoMap.insert("ObjectName",ObjectName);
    m_PropertyInfoMap[ObjectName.propertyName].setConnect();

    //位置
    GY_PropertyInfo positonPropertyInfo(this);
    positonPropertyInfo.propertyName="Position";
    positonPropertyInfo.propertyValue=QPointF(0,0);
    positonPropertyInfo.updateType = Update|MoveUpdate|PointOfRelative;
    positonPropertyInfo.propertyType = PointPropertyType;
    positonPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updatePosition;
    m_PropertyInfoMap.insert(positonPropertyInfo.propertyName,positonPropertyInfo);
    m_PropertyInfoMap[positonPropertyInfo.propertyName].setConnect();

    //隐藏
    GY_PropertyInfo isVisiblePropertyInfo(this);
    isVisiblePropertyInfo.propertyName="IsVisible";
    isVisiblePropertyInfo.propertyValue=true;
    isVisiblePropertyInfo.updateType = Update|OrdinaryAttributes;
    isVisiblePropertyInfo.propertyType = BoolPropertyType;
    isVisiblePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updateIsVisible;
    m_PropertyInfoMap.insert(isVisiblePropertyInfo.propertyName,isVisiblePropertyInfo);
    m_PropertyInfoMap[isVisiblePropertyInfo.propertyName].setConnect();

    //显示图形序号
    GY_PropertyInfo CurrentStatePropertyInfo(this);
    CurrentStatePropertyInfo.propertyName="CurrentState";
    CurrentStatePropertyInfo.propertyValue=0;
    CurrentStatePropertyInfo.updateType = Update|OrdinaryAttributes;
    CurrentStatePropertyInfo.propertyType = UshortPropertyType;
    CurrentStatePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updateCurrentState;
    m_PropertyInfoMap.insert(CurrentStatePropertyInfo.propertyName,CurrentStatePropertyInfo);
    m_PropertyInfoMap[CurrentStatePropertyInfo.propertyName].setConnect();

    //显示全部图形
    GY_PropertyInfo ShowAllStatesPropertyInfo(this);
    ShowAllStatesPropertyInfo.propertyName="ShowAllStates";
    ShowAllStatesPropertyInfo.propertyValue=false;
    ShowAllStatesPropertyInfo.updateType = Update|OrdinaryAttributes;
    ShowAllStatesPropertyInfo.propertyType = BoolPropertyType;
    ShowAllStatesPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updateShowAllStates;
    m_PropertyInfoMap.insert(ShowAllStatesPropertyInfo.propertyName,ShowAllStatesPropertyInfo);
    m_PropertyInfoMap[ShowAllStatesPropertyInfo.propertyName].setConnect();

    GY_PropertyInfo FallbackStateCapacityPropertyInfo(this);
    FallbackStateCapacityPropertyInfo.propertyName="FallbackState.Capacity";
    FallbackStateCapacityPropertyInfo.propertyValue=0;
    FallbackStateCapacityPropertyInfo.updateType = Update|OrdinaryAttributes;
    FallbackStateCapacityPropertyInfo.propertyType = IntPropertyType;
    FallbackStateCapacityPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updateFallbackStateCapacity;
    m_PropertyInfoMap.insert(FallbackStateCapacityPropertyInfo.propertyName,FallbackStateCapacityPropertyInfo);
    m_PropertyInfoMap[FallbackStateCapacityPropertyInfo.propertyName].setConnect();

    GY_PropertyInfo FallbackStateSizePropertyInfo(this);
    FallbackStateSizePropertyInfo.propertyName="FallbackState.Size";
    FallbackStateSizePropertyInfo.propertyValue=0;
    FallbackStateSizePropertyInfo.updateType = Update|OrdinaryAttributes;
    FallbackStateSizePropertyInfo.propertyType = IntPropertyType;
    FallbackStateSizePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updateFallbackStateSize;
    m_PropertyInfoMap.insert(FallbackStateSizePropertyInfo.propertyName,FallbackStateSizePropertyInfo);
    m_PropertyInfoMap[FallbackStateSizePropertyInfo.propertyName].setConnect();

    GY_PropertyInfo ActiveAreaCapacityPropertyInfo(this);
    ActiveAreaCapacityPropertyInfo.propertyName="ActiveArea.Capacity";
    ActiveAreaCapacityPropertyInfo.propertyValue=0;
    ActiveAreaCapacityPropertyInfo.updateType = Update|OrdinaryAttributes;
    ActiveAreaCapacityPropertyInfo.propertyType = IntPropertyType;
    ActiveAreaCapacityPropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updateActiveAreaCapacity;
    m_PropertyInfoMap.insert(ActiveAreaCapacityPropertyInfo.propertyName,ActiveAreaCapacityPropertyInfo);
    m_PropertyInfoMap[ActiveAreaCapacityPropertyInfo.propertyName].setConnect();

    GY_PropertyInfo ActiveAreaSizePropertyInfo(this);
    ActiveAreaSizePropertyInfo.propertyName="ActiveArea.Size";
    ActiveAreaSizePropertyInfo.propertyValue=0;
    ActiveAreaSizePropertyInfo.updateType = Update|OrdinaryAttributes;
    ActiveAreaSizePropertyInfo.propertyType = IntPropertyType;
    ActiveAreaSizePropertyInfo.updatePropertyFun=(GY_PropertyInfo::UpdatePropertyFun)&GY_Switch::updateActiveAreaSize;
    m_PropertyInfoMap.insert(ActiveAreaSizePropertyInfo.propertyName,ActiveAreaSizePropertyInfo);
    m_PropertyInfoMap[ActiveAreaSizePropertyInfo.propertyName].setConnect();
}

bool GY_Switch::updatePosition(const QString &name, const QVariant &value)
{
    if(!value.canConvert<QPointF>())
    {
        return false;
    }
    //setpos设置为父级坐标
    QPointF point = value.toPointF();
    if(this->parentItem()) {
        point = this->mapToParent(this->mapFromScene(value.toPointF()));
    }
    this->setPos(point);
    m_PropertyInfoMap[name].propertyValue=this->mapFromScene(value.toPointF());
    return true;
}

bool GY_Switch::updateIsVisible(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value;
    //switch是否显示
    this->setVisible(value.toBool());
    return true;
}

bool GY_Switch::updateCurrentState(const QString &name, const QVariant &value)
{
    if(!value.canConvert<ushort>())
    {
        return false;
    }
    //大于图形数按最大处理
    int m_OrderChildrensSize = m_OrderChildrens.size();
    if(value.toUInt()>=(ushort)m_OrderChildrensSize) {
        m_PropertyInfoMap[name].propertyValue=(ushort)m_OrderChildrensSize-1;
    }else {
        m_PropertyInfoMap[name].propertyValue=value.toUInt();
    }
    if(!m_PropertyInfoMap["ShowAllStates"].propertyValue.toBool()) {
        for(int i = 0; i<m_OrderChildrensSize; i++) {
            hideItem(i);
        }
        showChildrenItem(m_PropertyInfoMap[name].propertyValue.toUInt());
    }
    return true;
}

bool GY_Switch::updateShowAllStates(const QString &name, const QVariant &value)
{
    if(!value.canConvert<bool>())
    {
        return false;
    }
    m_PropertyInfoMap[name].propertyValue=value.toBool();
    int m_OrderChildrensSize = m_OrderChildrens.size();
    if(value.toBool()) {
        //全部显示
        for(int i = 0; i<m_OrderChildrensSize; i++) {
            showChildrenItem(i);
        }
    }else {
        //全部隐藏后显示一个
        for(int i = 0; i<m_OrderChildrensSize; i++) {
            hideItem(i);
        }
        showChildrenItem(m_PropertyInfoMap["CurrentState"].propertyValue.toUInt());
    }
    return true;
}

bool GY_Switch::updateFallbackStateCapacity(const QString &, const QVariant &)
{
    return false;
}

bool GY_Switch::updateFallbackStateSize(const QString &, const QVariant &)
{
    return false;
}

bool GY_Switch::updateActiveAreaCapacity(const QString &, const QVariant &)
{
    return false;
}

bool GY_Switch::updateActiveAreaSize(const QString &, const QVariant &)
{
    return false;
}

void GY_Switch::showChildrenItem(const int &index)
{
    if(index < 0) {
        return;
    }
    //从有序数组中取出图形
    GY_Object *pItem = m_OrderChildrens.at(index);
    if(pItem->inherits("GY_BasicsGraphicsObject"))
    {
        ((GY_BasicsGraphicsObject*)pItem)->setVisible(true);
    }
    else if(pItem->inherits("GY_ContainerTransform"))
    {
        ((GY_ContainerTransform*)pItem)->setVisible(true);
    }
}

void GY_Switch::hideItem(const int &index)
{
    if(index < 0) {
        return;
    }
    GY_Object *pItem = m_OrderChildrens.at(index);
    if(pItem->inherits("GY_BasicsGraphicsObject"))
    {
        ((GY_BasicsGraphicsObject*)pItem)->setVisible(false);
    }
    else if(pItem->inherits("GY_ContainerTransform"))
    {
        ((GY_ContainerTransform*)pItem)->setVisible(false);
    }
}
